const {
  $util,
} = getApp();
Page({
  data: {
    imgs: [],
    picture: [], // 上传图片
    learncontent: '', // 体会心得
  },
  onLoad(opts) {
    this.setData(opts, () => {
      this.loadDetail(opts.id);
    });
    wx.setNavigationBarTitle({
      title: opts.barTitle && decodeURIComponent(opts.barTitle) || '中心组学习详情',
    });
  },
  loadDetail(id) {
    $util.request({
      url: 'entry/wxapp/edu.centerlearn.getstudynote',
      data: {
        id,
      },
      success: (res) => {
        res.list.lastimg && res.list.lastimg.length && (res.list.imgs = res.list.lastimg);
        this.setData({
          ...res.list,
          loaded: true,
        });
      },
    });
  },
  bindIpVal(e) {
    this.setData({
      learncontent: e.detail.value,
    });
  },
  /**
   * 获取最新的 待提交的图片列表
   * @param {object} e 来自组件的数据
   * @param {string[]} e.detail
   * @return
   */
  imgsOnChange(e) {
    this.setData({
      imgs: e.detail,
    });
  },
  submit() {
    const D = this.data;
    if (D.learncontent.trim() === '') {
      wx.showModal({
        title: '提示',
        content: '请填写学习笔记',
        showCancel: false,
      });
      return;
    }
    $util.request({
      url: 'entry/wxapp/edu.centerlearn.setstudynote',
      data: {
        id: D.id,
        learncontent: D.learncontent,
        picture: D.imgs,
      },
      success: (res) => {
        wx.navigateBack();
      },
    });
  },
});
